package com.ljx.itbbs.server.article.dao;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljx.itbbs.common.domain.enums.CommonStatusEnum;
import com.ljx.itbbs.server.article.domain.entity.ArticleEntity;
import com.ljx.itbbs.server.article.domain.vo.req.ArticleListReq;
import com.ljx.itbbs.server.article.domain.vo.req.ArticleNewReq;
import com.ljx.itbbs.server.article.mapper.ArticleMapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class ArticleDao extends ServiceImpl<ArticleMapper, ArticleEntity> {
    public IPage<ArticleEntity> getArticleList(ArticleListReq req) {
        IPage<ArticleEntity> page = new Page<>(req.getCurrPage(), req.getPageSize());
        // 条件判断
        LambdaQueryWrapper<ArticleEntity> wrapper = new LambdaQueryWrapper<>();
        if (Objects.nonNull(req.getCategoryId())){
            wrapper.eq(ArticleEntity::getCategoryId, req.getCategoryId());
        }
        if (Objects.nonNull(req.getPlateId())){
            wrapper.eq(ArticleEntity::getPlateId, req.getPlateId());
        }
        if (Objects.nonNull(req.getUid())){
            wrapper.eq(ArticleEntity::getUid, req.getUid());
            wrapper.orderByAsc(ArticleEntity::getIsTop);
        }
        if (Objects.nonNull(req.getStatus())){
            wrapper.eq(ArticleEntity::getStatus, req.getStatus());
        } else {
            wrapper.eq(ArticleEntity::getStatus, CommonStatusEnum.NORMAL.getStatus());
        }
        wrapper.orderByDesc(ArticleEntity::getCreateTime, ArticleEntity::getUpdateTime);

        return this.baseMapper.selectPage(page, wrapper);
    }

    public List<ArticleEntity> getNewArticleInfo(ArticleNewReq req) {
        LambdaQueryWrapper<ArticleEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ArticleEntity::getStatus, CommonStatusEnum.NORMAL.getStatus());
        wrapper.orderByDesc(ArticleEntity::getCreateTime).last("LIMIT 5");
        if (Objects.nonNull(req.getPlateId())){
            wrapper.eq(ArticleEntity::getPlateId, req.getPlateId());
        }
        return this.baseMapper.selectList(wrapper);
    }

    public List<ArticleEntity> getHotArticleInfo() {
        return this.baseMapper.getHotArticleInfo();
    }

    public void increaseViewsNum(Long articleId) {
        lambdaUpdate().setSql("views_num = views_num + 1").eq(ArticleEntity::getId, articleId).update();
    }

    public void increaseCommentNum(Long articleId) {
        lambdaUpdate().setSql("comment_num = comment_num + 1").eq(ArticleEntity::getId, articleId).update();
    }

    public void increaseLikeNum(Long articleId) {
        lambdaUpdate().setSql("like_num = like_num + 1")
                .eq(ArticleEntity::getId, articleId)
                .update();
    }

    public void increaseCollectNum(Long articleId) {
        lambdaUpdate().setSql("collect_num = collect_num + 1")
                .eq(ArticleEntity::getId, articleId)
                .update();
    }

    public IPage<ArticleEntity> getArticleAdminList(ArticleListReq req) {
        IPage<ArticleEntity> page = new Page<>(req.getCurrPage(), req.getPageSize());
        // 条件判断
        LambdaQueryWrapper<ArticleEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(ArticleEntity::getCreateTime, ArticleEntity::getUpdateTime);
        if (Objects.nonNull(req.getCategoryId())){
            wrapper.eq(ArticleEntity::getCategoryId, req.getCategoryId());
        }
        if (Objects.nonNull(req.getPlateId())){
            wrapper.eq(ArticleEntity::getPlateId, req.getPlateId());
        }
        if (Objects.nonNull(req.getUid())){
            wrapper.eq(ArticleEntity::getUid, req.getUid());
        }
        if (CollectionUtil.isNotEmpty(req.getPlateIds())){
            wrapper.in(ArticleEntity::getPlateId, req.getPlateIds());
        }

        return this.baseMapper.selectPage(page, wrapper);
    }

    public void updateStatus(Long articleId, Integer status) {
        lambdaUpdate().set(ArticleEntity::getStatus, status)
                .eq(ArticleEntity::getId, articleId)
                .update();
    }

    public void reduceLikeNum(Long articleId) {
        lambdaUpdate().setSql("like_num = like_num - 1")
                .eq(ArticleEntity::getId, articleId)
                .update();
    }
}
